package com.molyfun.parents.activity.course.adapter;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.listactivity.viewholder.CourseListViewHolder;
import com.molyfun.parents.activity.course.view.CourseView;
import com.molyfun.parents.rest.bean.CourseListResponse;

import java.util.HashSet;
import java.util.List;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:9/19/16.
 * 主页
 */
public class CourseListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements CourseView.CourseViewCallback {
    private Context mContext;

    private List<CourseListResponse.CourseBean> mDataList;

    FragmentManager mFragmentManager;


    public CourseListAdapter(Context mContext) {
        this.mContext = mContext;
        this.mFragmentManager = null;
    }

    public CourseListAdapter(Context mContext, FragmentManager fragmentManager) {
        this.mContext = mContext;
        this.mFragmentManager = fragmentManager;
    }

    public void clearAll() {
        if (mDataList != null) {
            this.mDataList.clear();
        }
        notifyDataSetChanged();
    }

    public void addAll(List<CourseListResponse.CourseBean> list){
        mDataList.addAll(list);
        notifyDataSetChanged();
    }

    public void refreshData(List<CourseListResponse.CourseBean> courseBeanList) {
        this.mDataList = courseBeanList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new CourseListViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_course_list_cell, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        CourseListViewHolder courseViewHolder = (CourseListViewHolder) holder;
        courseViewHolder.mCourseView.initData(mDataList.get(position));
//        courseViewHolder.mCourseView.setCourseViewCallback(this);
    }

    private HashSet<CourseListResponse.CourseBean> mChoosedList = new HashSet<>();

    @Override
    public void onCourseCheckChanged(CourseListResponse.CourseBean courseBean, boolean isChecked) {
        if (isChecked) {
            mChoosedList.add(courseBean);
        } else {
            mChoosedList.remove(courseBean);
        }
    }

    @Override
    public int getItemCount() {
        if (mDataList == null) {
            return 0;
        }
        return mDataList.size();
    }

}
